package com.ml.doctor.fragment;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.reflect.TypeToken;
import com.ml.doctor.R;
import com.ml.doctor.activity.SubscribeActivity;
import com.ml.doctor.bean.YuYueBean;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.LocalShared;
import com.ml.doctor.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by kun on 17/12/2.
 */

public class YuYueFragment extends BaseRefreshLoadFragment {
    public static final int REFRESH_CODE = 2;
    List<YuYueBean> list;
    YuyueAdapter patienAdapter;
    int page = 1;
    int status;

    @Override
    protected void initView() {
        super.initView();
        list = new ArrayList<>();
        patienAdapter = new YuyueAdapter();
        status = getArguments().getInt("status", 2);
        swipeTarget.setAdapter(patienAdapter);
        swipeToLoadLayout.setRefreshing(true);
        swipeTarget.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getActivity(), SubscribeActivity.class);
                i.putExtra("yuyue", list.get(position));
                i.putExtra("status", status);
                startActivityForResult(i, REFRESH_CODE);
            }
        });
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        getData();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        page = 1;
        getData();
    }

    public void getData() {
        Map<String, String> map = new HashMap<>();
        map.put("docterid", LocalShared.getInstance(getActivity()).getUserId() + "");
        map.put("page", page + "");
        map.put("limit", 20 + "");
        map.put("state", status + "");
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "/bl/reserve_one_docter", map, new TypeToken<List<YuYueBean>>() {
        }.getType(), new NetworkManager.SuccessCallback<List<YuYueBean>>() {
            @Override
            public void onSuccess(List<YuYueBean> response) {
                if (page == 1) {
                    list.clear();
                }
                list.addAll(response);
                page++;
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
                patienAdapter.notifyDataSetChanged();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(getActivity(), message);
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
                if (page == 1) {
                    list.clear();
                    patienAdapter.notifyDataSetChanged();
                }

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REFRESH_CODE && requestCode == REFRESH_CODE) {
            swipeToLoadLayout.setRefreshing(true);
        }
    }


    class YuyueAdapter extends BaseAdapter {

        ViewHolder viewHolder;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.item_yuyue, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }
            viewHolder = (ViewHolder) convertView.getTag();
            Glide.with(getActivity()).load(list.get(position).user_photo).asBitmap().into(viewHolder.ivAvatar);
            viewHolder.txtName.setText(list.get(position).bname);
            viewHolder.txtTime.setText("预约时间：" + list.get(position).start_time + "-" + list.get(position).end_time.split(" ")[1]);
            return convertView;
        }


        class ViewHolder {
            @BindView(R.id.iv_avatar)
            ImageView ivAvatar;
            @BindView(R.id.txt_name)
            TextView txtName;
            @BindView(R.id.txt_time)
            TextView txtTime;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }

}
